const { appealDetail, filePreview, filePreviews } = require('../../../../../../api/index');
const { getBatchDict } = require('../../../../../../utils/dict');
const { sliceString } = require('../../../../../../utils/util');

// pages/active/AssistanceCenter/pages/detail/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    applyTypeDicts: [],
    applyProcessStateDicts: [],
    detail: {},
    steps: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log(options);
    await loadDict.call(this);
    await fetchDataList.call(this, options.id);
  }
})

//  请求数据
async function fetchDataList(id) {
  const { errors = [], appealRecord  } = await appealDetail({ id });
  if( errors.length > 0 ) { return; }
  this.setData({ detail: setResult.call( this, appealRecord ) })
}

function setResult(data) {
  const _data = JSON.parse(JSON.stringify(data));
  _data.appealPhotos = filePreviews( '/file/preview/', _data.appealPhoto);
  _data.statusClass = 'status-' + _data.processState;
  _data.appealType = this.data.applyTypeDicts.find(_s => _s.code === _data.appealType)?.name;
  _data.processState = this.data.applyProcessStateDicts.find(_s => _s.code === _data.processState )?.name;
  _data.appealContent = sliceString(_data.appealContent, 29);
  
  return _data;
}

//  加载页面所需要的 - 
async function loadDict() {
  const [ applyTypeDicts, applyProcessStateDicts ] = await getBatchDict(['APPEAL_TYPE', 'APPEAL_PROCESS_STATE']);
  this.data.applyTypeDicts = applyTypeDicts || [];
  this.data.applyProcessStateDicts = applyProcessStateDicts || [];
}